package com.lgkj.ui.shop;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.HavestOrderAdapter;
import com.lgkj.bean.meitao.HarvestBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.HttpUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/12/7 0007.
 */

public class ComfirHarvestOrderActivity extends BaseActivity {
    @BindView(R.id.tv_yundan)
    TextView tvYundan;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_item)
    ListView llItem;
    private String orderNo = "";
    private HarvestBean bean;
    private String phone = "";
    private HavestOrderAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_confir_havest;
    }

    @Override
    public void initView() {
        setTitle("物流信息");
        orderNo = getIntent().getStringExtra("orderNo");
        adapter = new HavestOrderAdapter(this);
    }

    @Override
    public void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("order_info_id", "1");
        HttpUtils.postHttp(map, CommonConstant.geExpressInfo, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    bean = new Gson().fromJson(jsonObject.toString(), HarvestBean.class);
                    fillData();
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    private void fillData() {
        if (bean != null && bean.getData() != null) {
            tvYundan.setText("运单号:" + bean.getData().getNo());
            tvType.setText("快递:" + bean.getData().getName());
            phone = bean.getData().getPhone();
            tvPhone.setText("电话:" + phone);
            llItem.setAdapter(adapter);
            if (bean.getData().getList() != null) {
                adapter.setDataBeans(bean.getData().getList());
            }
            adapter.notifyDataSetChanged();
        }
    }

    @OnClick({R.id.btn_save, R.id.tv_phone})
    public void onClick(View view) {
        if (R.id.btn_save == view.getId()) {
            showDiaLog("正在收货");
            Map<String, String> map = new HashMap<>();
            map.put("access_token", getToken());
            map.put("order_info_id", orderNo);
            HttpUtils.postHttp(map, CommonConstant.confirmOrder, new HttpUtils.HttpCallBack() {
                @Override
                public void successHttp(JSONObject jsonObject, int id) {
                    dismissDiaLog();
                    if (jsonObject.optInt("code") == 0) {
                        toastLong("收货成功", 0);
                        finish();
                    } else {
                        toastLong(jsonObject.optString("msg"), 0);
                    }
                }

                @Override
                public void errorHttp(String msg) {
                    dismissDiaLog();
                }
            });
        } else if (view.getId() == R.id.tv_phone) {
            Uri data = Uri.parse("tel:" + phone);
            Intent intent1 = new Intent(Intent.ACTION_CALL);
            intent1.setData(data);
            if (phone != null && phone.length() > 0)
                startsActivity(intent1);
        }
    }

}
